package com.wiseman.runner;

import cn.domob.android.ads.DomobAdManager.ErrorCode;
import cn.domob.android.ads.DomobInterstitialAd;
import cn.domob.android.ads.DomobInterstitialAdListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.drawable.Drawable.Callback;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class MyPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener, ConstValue, OnPreferenceClickListener {

	//Preference mLocationMethod = null;
	Preference mIntervalTime = null;
	Preference mIntervalDistance = null;
	Preference mBackupReference = null;
	Preference mRestoreReference = null;
	private DomobInterstitialAd mInterstitialAd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.setting);

		//mLocationMethod = getPreferenceScreen().findPreference(KEY_LOCATION_METHOD);
		PreferenceScreen screen = getPreferenceScreen();

		mIntervalTime = screen.findPreference(KEY_INTERVAL_TIME);
		mIntervalDistance = screen.findPreference(KEY_INTERVAL_DISTANCE);
		
		mBackupReference = screen.findPreference(KEY_BACKUP);
		mBackupReference.setOnPreferenceClickListener(this);
		
		mRestoreReference = screen.findPreference(KEY_RESTORE);
		mRestoreReference.setOnPreferenceClickListener(this);
		
		SharedPreferences sp = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
		
	//	String vstr = sp.getString(KEY_LOCATION_METHOD, "GPS");
	//	mLocationMethod.setSummary(vstr);	
		long intervalTime;
		long intervalDistance;
		String vstr = sp.getString(KEY_INTERVAL_TIME, Integer.toString(DEFAULT_INTERVAL_TIME));
		try {
			intervalTime = Long.parseLong(vstr);
		} catch (NumberFormatException e) {
			intervalTime = DEFAULT_INTERVAL_TIME;
		}		
		
		mIntervalTime.setDefaultValue(intervalTime);	
		mIntervalTime.setSummary(vstr);

		vstr = sp.getString(KEY_INTERVAL_DISTANCE, Integer.toString(DEFAULT_INTERVAL_DISTANCE));
		try {
			intervalDistance = Long.parseLong(vstr);
		} catch (NumberFormatException e) {
			intervalDistance = DEFAULT_INTERVAL_DISTANCE;
		}
		
		mIntervalDistance.setDefaultValue(intervalDistance);
		mIntervalDistance.setSummary(vstr);						
		
		mInterstitialAd = DomoAd.createInterstitialAd(this);	
	}

	@Override
	protected void onResume() {
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		super.onResume();
	}

	@Override
	protected void onPause() {
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		super.onPause();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		//
		String vstr = null;
		if (KEY_LOCATION_METHOD.equals(key)) {
//			vstr = sharedPreferences.getString(key, "GPS");
//			mLocationMethod.setSummary(vstr);
		} else if (KEY_INTERVAL_TIME.equals(key)) {
			vstr = sharedPreferences.getString(key, Integer.toString(DEFAULT_INTERVAL_TIME));
			mIntervalTime.setSummary(vstr);
		} else if (KEY_INTERVAL_DISTANCE.equals(key)) {
			vstr = sharedPreferences.getString(key, Integer.toString(DEFAULT_INTERVAL_DISTANCE));
			mIntervalDistance.setSummary(vstr);
		} else if (KEY_BACKUP.equals(key)) {
		
			// do backup
		} else if (KEY_RESTORE.equals(key)) {
			// do restore.
			new BackupTask(this).execute(COMMAND_RESTORE);
		}

		Log.i("Runner", "key = " + key + "value = " + vstr);

	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		if(KEY_BACKUP.equals(preference.getKey())){
			DomoAd.showInterstitialAd(this, mInterstitialAd);
			new BackupTask(this).execute(COMMAND_BACKUP);
		}else if(KEY_RESTORE.equals(preference.getKey())){
			DomoAd.showInterstitialAd(this, mInterstitialAd);
			new BackupTask(this).execute(COMMAND_RESTORE);
		}

		return false;
	}
		

}
